import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:sph_shipper/http/api.dart';

import '../../AppConfig/Config.dart';
import '../../http/NoLoadingGetApi.dart';

class TMSOrderDetial extends StatefulWidget {
  const TMSOrderDetial({super.key, required this.dataMap});
  final Map dataMap;
  @override
  State<TMSOrderDetial> createState() => _TMSOrderDetialState();
}

class _TMSOrderDetialState extends State<TMSOrderDetial> {
  Map dataMap={};
  String reasonStr="";
  void _copyToClipboard(String copyNumber) {
    Clipboard.setData(ClipboardData(text: copyNumber));
    BrnToast.show('复制成功', context);

  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDetialAct();
  }

  void getDetialAct(){
    NoLoadinggetApiManager.getData(
      context,
      "api/tms/order/detail",
      {
       "id":widget.dataMap['id']
      },
          (responseData) async {
        setState(() {
         dataMap=responseData['data'];
        });

      },
    );
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        title: Text("订单详情",style: TextStyle(fontWeight: FontWeight.w300,fontSize: 16.sp,color: Colors.black),),
        centerTitle: true,
        backgroundColor: Color.fromRGBO(251,236,228,1),
        leading: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black,),onPressed: (){Navigator.pop(context);},),
      ),
      body:dataMap.isEmpty? Center(
        child:
        LoadingAnimationWidget.prograssiveDots(color: MyColors.themeColor, size: 50),
      ): Container(
      decoration: BoxDecoration(
      gradient: LinearGradient(
    begin: Alignment.topCenter,
    end: Alignment.bottomCenter,
    colors: [Color.fromRGBO(251,236,228,1),MyColors.backgreyColor, MyColors.backgreyColor],
    ),
    ),
        child: Column(
          children: [
            Expanded(child: ListView(children: [
              Container(
                padding: EdgeInsets.only(left: 23.w,right: 45.w),
                child:  Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(dataMap['status']==0?"待调度":"已作废",style: TextStyle(color: MyColors.themeColor,fontSize: 28.sp,fontWeight: FontWeight.w600),),
                        SizedBox(width: 10.w,),
                        if(dataMap['status']=="ABNORMAL")  Text("订单异常",style: TextStyle(color: Colors.red,fontSize: 13.sp),),
                        ],
                      ),

                    ),

                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
                margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("货运信息",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                    SizedBox(height: 5.h,),
                    Row(
                      children: [
                        Column(
                          children: [
                            Image.asset("lib/Images/OrderList/装货点icon.png",width:20.w ,height:20.h,),
                            Image.asset("lib/Images/OrderList/垂直虚线.png",width:15.w ,height:20.h,),
                            Image.asset("lib/Images/OrderList/卸货点icon.png",width:20.w ,height:20.h,),
                          ],
                        ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Container(
                                    constraints: BoxConstraints(
                                      maxWidth:180.w, // 限制文本最大宽度
                                    ),
                                    child: Text(dataMap['consignorInfo']['place']['province']+dataMap['consignorInfo']['place']['city']+dataMap['consignorInfo']['place']['county'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp,fontWeight: FontWeight.w600,overflow: TextOverflow.ellipsis),),
                                  ),
                                  SizedBox(width: 4.w,),
                                  Container(
                                    constraints: BoxConstraints(
                                      maxWidth:screenSize.width-80.w, // 限制文本最大宽度
                                    ),
                                    child: Text(dataMap['loadDay']+" "+dataMap['loadTime']+"点装货",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp,overflow: TextOverflow.ellipsis),),
                                  ),
                                ],
                              ),
                              Text(dataMap['consignorInfo']['place']['address'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                              SizedBox(height: 4.h,),
                              Row(
                                children: [
                                  Container(
                                    constraints: BoxConstraints(
                                      maxWidth:180.w, // 限制文本最大宽度
                                    ),
                                    child: Text(dataMap['consigneeInfo']['place']['province']+dataMap['consigneeInfo']['place']['city']+dataMap['consigneeInfo']['place']['county'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp,fontWeight: FontWeight.w600,overflow: TextOverflow.ellipsis),),
                                  ),
                                  SizedBox(width: 4.w,),
                                  Container(
                                    constraints: BoxConstraints(
                                      maxWidth:screenSize.width-80.w, // 限制文本最大宽度
                                    ),
                                    child: Text(dataMap['unloadDay']+" "+dataMap['unloadTime']+"点卸货",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp,overflow: TextOverflow.ellipsis),),
                                  ),
                                ],
                              ),
                              Text(dataMap['consigneeInfo']['place']['address'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                            ],
                          ),
                      ],
                    ),
                    SizedBox(height: 10.h,),
                    Row(
                      children: [
                        Text("全程",style: TextStyle(color: MyColors.textColor,fontSize: 12.sp),),

                        Text(dataMap['distance']!=0? (int.parse("10000")/1000).toStringAsFixed(2):"--" ,style: TextStyle(color: MyColors.themeColor,fontSize: 12.sp),),
                        Text("公里",style: TextStyle(color: MyColors.textColor,fontSize: 12.sp),),
                      ],
                    ),
                    Divider(height: 16.h,color: MyColors.backgreyColor,),

                    Row(
                      children: [
                        Image.asset("lib/Images/OrderList/包裹icon.png",width: 24.w,height: 24.h,),
                          Text(dataMap['cargoInfo']['name']+"  "+dataMap['cargoInfo']['weight'].toString()+dataMap['cargoInfo']['weightUnit'].toString(),style: TextStyle(color: Colors.black,fontSize: 12.sp),),
                      ],
                    ),

                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
                margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("发货单位",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                        Text(dataMap['consignorInfo']['name'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                      ],
                    ),
                    SizedBox(height: 4.h,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("收货单位",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                        Text(dataMap['consigneeInfo']['name'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                      ],
                    ),
                    SizedBox(height: 4.h,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("订单时间",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                        Text(dataMap['createTime'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                      ],
                    ),
                    SizedBox(height: 4.h,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("订单来源",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                        Text(dataMap['sourceType']=="INPUT"?"录入":"三方数据对接",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                      ],
                    ),
                    SizedBox(height: 4.h,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("订单号",style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                        GestureDetector(
                          child: Row(
                            children: [
                              Text(dataMap['orderNum'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                              Text("复制",style: TextStyle(color: MyColors.themeColor,fontSize: 13.sp),),
                            ],
                          ),
                          onTap: (){
                            _copyToClipboard(dataMap['orderNum']);
                          },
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
            ),
            ),
            Container(
              padding: EdgeInsets.only(right: 10.w,top: 10.h,bottom: 35.h),
              height: 80.h,
              color: Colors.white,
              width: screenSize.width,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  GestureDetector(
                    child: BrnTagCustom(
                      tagText: "调度",
                      textColor: Colors.white,
                      backgroundColor: MyColors.themeColor,
                      tagBorderRadius: BorderRadius.all(Radius.circular(30)),
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w600,
                      textPadding: EdgeInsets.only(left: 12.w,right: 12.w,top: 2.h,bottom: 2.h),
                    ),
                  ),
                  SizedBox(width: 4.w,),
                  GestureDetector(
                    child: BrnTagCustom(
                      tagText: "修改",
                      textColor: Colors.white,
                      backgroundColor: MyColors.themeColor,
                      tagBorderRadius: BorderRadius.all(Radius.circular(30)),
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w600,
                      textPadding: EdgeInsets.only(left: 12.w,right: 12.w,top: 2.h,bottom: 2.h),
                    ),

                  ),
                  SizedBox(width: 4.w,),
                  GestureDetector(
                    child: BrnTagCustom(
                      tagText: dataMap['state']=="NORMAL"?"标记异常":"取消异常标记",
                      textColor: Colors.white,
                      backgroundColor: MyColors.themeColor,
                      tagBorderRadius: BorderRadius.all(Radius.circular(30)),
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w600,
                      textPadding: EdgeInsets.only(left: 12.w,right: 12.w,top: 2.h,bottom: 2.h),
                    ),
                    onTap: (){
                      showDialog(
                        barrierDismissible: false,
                        context: context,
                        builder: (BuildContext context) {
                          return BrnDialog(
                            titleText: dataMap['state']=="NORMAL"?"标记异常":"取消异常标记",
                            contentWidget:BrnInputText(
                              maxHeight: 100,
                              minHeight: 100,
                              minLines:2,
                              maxLength: 200,
                              bgColor: MyColors.backgreyColor,
                              textString:reasonStr ?? '',
                              textInputAction: TextInputAction.newline,
                              // maxHintLines: 20,
                              hint: '其他原因',
                              padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                              onTextChange: (text) {
                                print(text);

                                setState(() {
                                  reasonStr = text;
                                });
                              },
                              onSubmit: (text) {
                                print(text);
                              },
                            ),
                            actionsWidget: [
                              TextButton(
                                onPressed: () {
                                Navigator.pop(context);
                                },
                                child: Text("取消",style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                              ),
                              TextButton(
                                onPressed: () {
                                  ApiManager.postData(
                                    context,
                                    'api/user/shpr/user/searchAirCarList',
                                    {
                                      "id": dataMap['id'],
                                      "state": dataMap['state']=="NORMAL"?"ABNORMAL":"NORMAL",
                                      "stateReason": reasonStr,
                                    },
                                        (responseData) {
                                          BrnToast.show("操作成功", context);
                                          Navigator.pop(context);
                                          getDetialAct();
                                    },
                                  );
                                },
                                child: Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                              ),

                            ],
                          );
                        },
                      );
                    },
                  ),

                ],
              ),
            ),
          ],
        ),
      ) ,
    );
  }
}
